package cn.edu.pzhu.javaweb.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.pzhu.javaweb.pojo.Result;
import cn.edu.pzhu.javaweb.pojo.User;
import cn.edu.pzhu.javaweb.service.UserService;
import cn.edu.pzhu.javaweb.service.imp.UserServiceImp;
import cn.edu.pzhu.javaweb.util.Conver2MD5;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private UserService userService = new UserServiceImp();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if(username==null || username.isEmpty() || password ==null || password.isEmpty()) {
			response.getWriter().print("<script>alert('信息不完整!');location.href='login.jsp';</script>");	
			return;
		}
		String sec_password = Conver2MD5.getSHA256(Conver2MD5.getSHA256(username+password)+"pzhu");
		User user = new User(username,sec_password,null);
		Result<String> result = userService.login(user);
		if(result.getCode()==0) {
			String isSave = request.getParameter("save");
			if(isSave ==null||isSave.isEmpty()) {
				Cookie usernameCookie = new Cookie("username","");
				usernameCookie.setMaxAge(0);
				response.addCookie(usernameCookie);
				
				Cookie passwordCookie = new Cookie("password","");
				usernameCookie.setMaxAge(0);
				response.addCookie(passwordCookie);
				
				Cookie saveCookie = new Cookie("save","");
				usernameCookie.setMaxAge(0);
				response.addCookie(saveCookie);
			}else {
				Cookie usernameCookie = new Cookie("username",username);
				usernameCookie.setMaxAge(7*24*60*60);
				response.addCookie(usernameCookie);
				
				Cookie passwordCookie = new Cookie("password",password);
				usernameCookie.setMaxAge(7*24*60*60);
				response.addCookie(passwordCookie);
				
				Cookie saveCookie = new Cookie("save","yes");
				usernameCookie.setMaxAge(7*24*60*60);
				response.addCookie(saveCookie);
			}
			
			
				request.getSession().setAttribute("username", username);
				response.getWriter().print("<script>alert('登录成功');location.href='userinfo';</script>");
			
			}else {
			response.getWriter().print("<script>alert('"+result.getMessage()+"');location.href='login.jsp';</script>");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
